package jp.co.dailyReport.search.action;

import java.util.Calendar;
import java.util.List;
import java.util.ArrayList;
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpSession;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.log4j.Logger;
import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.DynaActionForm;
import jp.co.dailyReport.weeklyReport.WeeklyReport;
import jp.co.dailyReport.weeklyReport.WeeklyReportManager;
import jp.co.dailyReport.weeklyReport.WeeklyReportWorkItem;
import jp.co.dailyReport.weeklyReport.WeeklyReportWorkItemManager;
import jp.co.dailyReport.workItem.WorkItemManager;
import jp.co.dailyReport.staff.Staff;
import jp.co.dailyReport.staff.StaffManager;
import jp.co.dailyReport.project.Project;
import jp.co.dailyReport.project.ProjectManager;
import jp.co.dailyReport.project.PjStaff;
import jp.co.dailyReport.project.PjStaffManager;
import jp.co.dailyReport.DataUnavailableException;

/**
 * 週報検索の検索結果一覧ページのプロジェクト名のリンク
 * が押されたときの処理を管理するクラスです。
 */
public class SearchWeeklyReportInfoAction extends Action {

	private static Logger log = Logger.getLogger(SearchWeeklyReportInfoAction.class);

	/**
	 * 週報検索の検索結果一覧ページのプロジェクト名のリンク
	 * が押されたときの処理を実行します。<BR>
	 * プロジェクトリーダーの社員情報、プロジェクトメンバーの社員情報、作業項目情報を取得します。
	 * 週報IDから週報情報を取得し、DynaActionFormオブジェクトのフィールドに設定します。
	 * 
	 * @param mapping このインスタンスを選択するために使用したActionMapping
	 * @param form 存在するならば、このリクエストのためのActionForm Bean
	 * @param request 処理しているHTTPリクエスト
	 * @param response 処理しているHTTPレスポンス
	 * @return ActionForwardインスタンス
	 * @throws IOException, ServletException
	 */
	public ActionForward execute(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response)
		 throws IOException, ServletException {

		log.debug("SearchWeeklyReportInfoAction start");
		HttpSession session = request.getSession();
		log.debug("session.getId() = " + session.getId());

		DynaActionForm doWeeklyReportActionForm = (DynaActionForm) form;
		WeeklyReport weeklyReport = null;
		Project project = null;
		Staff pLStaff = null;
		List staffList = new ArrayList();
		WeeklyReportWorkItem[] weeklyReportWorkItems = null;
		List workItemList = new ArrayList();

		try {
			int weeklyReportId = ((Integer)doWeeklyReportActionForm.get("weeklyReportId")).intValue();
			log.debug("Search info weeklyReportId: " + weeklyReportId);

			weeklyReport = WeeklyReportManager.load(weeklyReportId);
			int projectId = weeklyReport.getProjectId();
			project = ProjectManager.load(projectId);
			pLStaff = StaffManager.load(project.getPLStaffId());
			PjStaff[] pjStaff = PjStaffManager.getStaffId(projectId);
			for(int i=0; i < pjStaff.length ; i++){
				int staffId = pjStaff[i].getStaffId();
				staffList.add(StaffManager.load(staffId));
			}
			weeklyReportWorkItems = WeeklyReportWorkItemManager.load(weeklyReportId);

			for(int i=0; i < weeklyReportWorkItems.length ; i++){
				int workItemId = weeklyReportWorkItems[i].getWorkItemId();
				workItemList.add(WorkItemManager.load(workItemId));
			}
		} catch(DataUnavailableException due) {
			log.error(due.getMessage());
		}
		doWeeklyReportActionForm.set("weeklyReportId", new Integer(weeklyReport.getWeeklyReportId()));
		doWeeklyReportActionForm.set("projectId", new Integer(weeklyReport.getProjectId()));
		doWeeklyReportActionForm.set("staffId", new Integer(weeklyReport.getStaffId()));

		Calendar workDateStart = weeklyReport.getWorkDateStart();
		doWeeklyReportActionForm.set("workDateStartY", new Integer(workDateStart.get(Calendar.YEAR)));
		doWeeklyReportActionForm.set("workDateStartM", new Integer(workDateStart.get(Calendar.MONTH)+1));
		doWeeklyReportActionForm.set("workDateStartD", new Integer(workDateStart.get(Calendar.DATE)));

		Calendar workDateEnd = weeklyReport.getWorkDateEnd();
		doWeeklyReportActionForm.set("workDateEndY", new Integer(workDateEnd.get(Calendar.YEAR)));
		doWeeklyReportActionForm.set("workDateEndM", new Integer(workDateEnd.get(Calendar.MONTH)+1));
		doWeeklyReportActionForm.set("workDateEndD", new Integer(workDateEnd.get(Calendar.DATE)));

		doWeeklyReportActionForm.set("totalHoursWorked", new Double(weeklyReport.getTotalHoursWorked()));
		doWeeklyReportActionForm.set("problem", weeklyReport.getProblem());
		doWeeklyReportActionForm.set("comment", weeklyReport.getComment());

		request.setAttribute("project", project);
		request.setAttribute("pLStaff", pLStaff);
		request.setAttribute("staffList", staffList);
		request.setAttribute("weeklyReportWorkItem", weeklyReportWorkItems);
		request.setAttribute("workItemList", workItemList);

		log.debug("SearchWeeklyReportInfoAction end");
		return mapping.findForward("info");
	}
}
